package com.ruoyi.project.user.company.service.impl;

import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.framework.aspectj.lang.annotation.Excel;
import com.ruoyi.framework.shiro.service.PasswordService;
import com.ruoyi.project.system.user.domain.User;
import com.ruoyi.project.system.user.service.IUserService;
import com.ruoyi.project.user.company.domain.Company;
import com.ruoyi.project.user.company.mapper.CompanyMapper;
import com.ruoyi.project.user.company.service.CompanyService;
import org.apache.poi.hssf.record.DVALRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 *
 * 婚庆公司  业务层
 *
 * @author xf
 */
@Service
public class CompanyServiceImpl implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;
    @Autowired
    private PasswordService passwordService;

    @Autowired
    private IUserService iUserService;
    @Override
    public List<Company> selectAll(Company company) {

        return companyMapper.selectCompany(company);
    }

    @Override
    public Long checkCompanyPhone(String companyPhone) {

        return companyMapper.checkCompanyPhone(companyPhone);
    }

    @Override
    public Long checkCompanyEmail(String companyEmail) {
        return companyMapper.checkCompanyEmail(companyEmail);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int insertCompany(Company company, String confirmPassword) {

        Date date = new Date();
        //添加user表

        User user = new User();
        user.setEmail(company.getCompanyEmail());
        user.setLoginName(company.getCompanyPhone());
        user.setUserName(company.getCompanyName());
        user.setPassword(confirmPassword);
        //新添加的公司默认都是停用状态
        user.setStatus("1");
        user.setPhonenumber(company.getCompanyPhone());
        int index = iUserService.insertUser(user);
        if(index > 0){
            //添加到公司表
            company.setCompanyUserId(user.getUserId());
            company.setCompanyDate(date);
            int index2 = companyMapper.insertCompany(company);
            if(index2 >= 0){
                return index2;
            }
        }
        return 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int deleteCompany(Long[] ids) {

        int index = 0;
        //删除user表中的数据
        for (Long id : ids) {
            Company company = companyMapper.selectByIdCompany(id);
            index += iUserService.deleteUserById(company.getCompanyUserId());

        }
        //再删除公司表中的数据

        if (index == ids.length){
            int index2 = companyMapper.deleteCompany(ids);
            return index2;
        }

        return 0;
    }

    @Override
    public List<Company> selectAllExcel(Company company, Integer order) {
        return companyMapper.selectCompanyExcel(company,order);
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public int updateStateCompany(Company company) {

        Company companys = companyMapper.selectByIdCompany(Long.parseLong(company.getCompanyId() + ""));
        companys.getCompanyUserId();
        User user = new User();
        user.setUserId(companys.getCompanyUserId());
        user.setStatus(companys.getCompanyState() == 2?"0":"1");
        user.setUpdateBy(ShiroUtils.getLoginName());
        int index = iUserService.updateUserInfo(user);
        System.out.println(index);
        if(index > 0 ){
            int index2 = companyMapper.updateCompany(company);
            if (index2 >0 ){
                return index2;
            }
        }

        return 0;
    }

    @Override
    public Company selectOneByCompany(Long companyId) {
        return companyMapper.selectByIdCompany(companyId);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Long checkCompanyPassword(String beforePassword, Long companyId) {
        //根据Id查询对应的公司 获取在user表中的id
        Company company = companyMapper.selectByIdCompany(companyId);
        company.getCompanyUserId();
        //根据id查询user表中对应的对象
        User user = iUserService.selectUserById(company.getCompanyUserId());
        String password = passwordService.encryptPassword(user.getLoginName(),beforePassword,user.getSalt());
        if(!user.getPassword().equals(password)){
            return 1L;
        }
        return 0L;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public int changeCompany(Company company,String newPassword) {

        //根据修改user表
        Company user_company_id = companyMapper.selectByIdCompany(Long.parseLong(company.getCompanyId() + ""));
        User user = new User();
        user.setUserId(user_company_id.getCompanyUserId());
        user.setPhonenumber(company.getCompanyPhone());
        user.setUserName(company.getCompanyName());
        user.setLoginName(company.getCompanyPhone());
        user.setEmail(company.getCompanyEmail());
        user.randomSalt();
        String s = new PasswordService().encryptPassword(company.getCompanyPhone(), newPassword, user.getSalt());
        System.out.println(s);
        user.setPassword(s);

        int index_user = iUserService.updateUserInfo(user);
        if(index_user > 0 ){
            //修改company表
            int index_company = companyMapper.updateCompany(company);
            return index_company;
        }

        return 0;
    }

    @Override
    public int updateUserStates(Company company) {

        return companyMapper.updateCompany(company);
    }

    @Override
    public Company selectByUserId(Integer userId) {
        return companyMapper.selectByUserId(userId);
    }


}
